package com.example.tnot.Teacher.Homepart.InternalFragment.Frequent.NotManage.Adapter;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.tnot.R;
import com.example.tnot.Teacher.Homepart.InternalFragment.Frequent.NotManage.ItemBean.NotManage;
import com.example.tnot.Util.Dialog.CustomDialog;

import java.util.List;

public class NotManageAdapter extends RecyclerView.Adapter<NotManageAdapter.ViewHolder> {
    List<NotManage> notManageList;
    CustomDialog customDialog;
    public NotManageAdapter(List<NotManage> notManageList) {
        this.notManageList = notManageList;
    }
    @NonNull
    @Override
    public NotManageAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv_not_manage,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NotManageAdapter.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tv_todo_content.setText(notManageList.get(position).getContent());
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                // 创建 AlertDialog.Builder 对象
                AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("提示");
                builder.setMessage("确定要删除这条记录吗？");

                // 设置“是”按钮的点击事件
                builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 删除数据源中的条目
                        notManageList.remove(position);
                        // 通知适配器数据已更改
                        notifyItemRemoved(position);
                        // 可选：调整后续项目的索引
                        notifyItemRangeChanged(position, notManageList.size());
                    }
                });

                // 设置“否”按钮的点击事件（可选）
                builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // 关闭对话框不做任何操作
                        dialog.dismiss();
                    }
                });

                // 显示对话框
                builder.show();

                return true; // 消耗该事件
            }
        });
    }

    @Override
    public int getItemCount() {
        return notManageList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView tv_todo_content;
        CheckBox cb_todo;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tv_todo_content = itemView.findViewById(R.id.tv_todo_content);
            cb_todo = itemView.findViewById(R.id.cb_todo);
        }
    }


}
